using QAssetBundle;
using UnityEngine;
using UnityEngine.UI;
using QFramework;

namespace ProjectSurvivor
{
	public class UIGamePanelData : UIPanelData
	{
	}
	public partial class UIGamePanel : UIPanel
	{
		public static EasyEvent FlashScreen = new EasyEvent();
		public static EasyEvent OpenTreasureChestPanel = new EasyEvent();
		protected override void OnInit(IUIData uiData = null)
		{
			mData = uiData as UIGamePanelData ?? new UIGamePanelData();
			
			// 监听检验值变更 -- 初始化时也调用一次
			Global.Exp.RegisterWithInitValue(exp =>
			{
				ExpValue.FillAmount(exp / (float)Global.ExpToNextLevel());
				// ExpText.text = $"经验值：({exp}/{Global.ExpToNextLevel()})";
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			// 监听等级变更 -- 初始化时也调用一次
			Global.Level.RegisterWithInitValue(lv =>
			{
				LevelText.text = $"等级：{lv}";
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			// 监听等级变更
			Global.Level.Register(lv =>
			{
				Time.timeScale = 0; // 暂停游戏
				ExpUpgradePanel.Show();
				AudioKit.PlaySound(Sfx.LEVELUP);
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			// 每5点经验值升一级
			Global.Exp.RegisterWithInitValue(exp =>
			{
				if (exp >= Global.ExpToNextLevel())
				{
					Global.Exp.Value -= Global.ExpToNextLevel();
					Global.Level.Value++;
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			// 监听时间变化
			Global.CurrentSeconds.RegisterWithInitValue(currentSeconds =>
			{
				// 每30帧更新一下文本
				if (Time.frameCount % 30 == 0)
				{
					int currentSecondsInt = Mathf.FloorToInt(currentSeconds);
					int seconds = currentSecondsInt % 60;
					int minutes = currentSecondsInt / 60;
				
					TimeText.text = $"时间：{minutes:00}:{seconds:00}";
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			// 监听敌人数量变化
			EnemyGenerator.EnemyCount.RegisterWithInitValue(enemyCount =>
			{
				EnemyCountText.text = $"敌人：{enemyCount}";
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			// 默认隐藏
			ExpUpgradePanel.Show(); // 初始化
			ExpUpgradePanel.Hide();
			TreasureChestPanel.Hide();

			EnemyGenerator enemyGenerator = FindObjectOfType<EnemyGenerator>();
			// 计时
			ActionKit.OnUpdate.Register(() =>
			{
				Global.CurrentSeconds.Value += Time.deltaTime;
				
				// 通关
				if (enemyGenerator.CurrentWave == null && enemyGenerator.LastWave && EnemyGenerator.EnemyCount.Value == 0)
				{
					AudioKit.PlaySound(Sfx.GAMEPASS);
					UIKit.OpenPanel<UIGamePassPanel>();
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			// 金币
			Global.Coin.RegisterWithInitValue(coin =>
			{
				CoinText.text = $"金币：{coin}";
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			// 闪屏
			FlashScreen.Register(() =>
			{
				ActionKit.Sequence()
					.Lerp(0f, 0.5f, 0.1f, alpha =>
					{
						ScreenColor.ColorAlpha(alpha);
					})
					.Lerp(0.5f, 0f, 0.1f, alpha =>
					{
						ScreenColor.ColorAlpha(alpha);
					}, () => ScreenColor.ColorAlpha(0))
					.Start(this);
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			// 打开宝箱
			OpenTreasureChestPanel.Register(() =>
			{
				Time.timeScale = 0f;
				TreasureChestPanel.Show();
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
		}
		
		protected override void OnOpen(IUIData uiData = null)
		{
		}
		
		protected override void OnShow()
		{
		}
		
		protected override void OnHide()
		{
		}
		
		protected override void OnClose()
		{
		}
	}
}
